// 树结构初始化加载
var setting = {
	view : {
		selectedMulti : false
	},
	data : {
		key : {
			title : treeTitle,
			name : treeTitle
		},
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "pid",
			rootPId : 0
		}
	},
	callback : {
		onClick : function(event, treeId, treeNode) {
			var treeId = treeNode.id;
			var treeName = treeNode[treeTitle];
			$("#treeId").val(treeId);
			$("#treeName").val(treeName);
		}
	}
}, tree, loadTree = function() {
	$.get(treeUrl, function(data) {
		data = data.data;
		if(_.isEmpty(data)){
			$("#treeEmpty").text("暂无数据信息");
		}
		var treeName = $("#treeName").val();
		tree = $.fn.zTree.init($("#tree"), setting, data);
		// 展开第一级节点
		var nodes = tree.getNodesByParam("level", 0);
		for (var i = 0; i < nodes.length; i++) {
			tree.expandNode(nodes[i], false, false, false);
			selectCheckNode(treeName, nodes[i]);
		}
		// 初始化时 如果是修改时，将找到对应的树 ，然后展开

	}, null, null, "正在加载，请稍后...");
};

$(function() {
	loadTree();

});

function selectCheckNode(treeName, node) {
	if (treeName == node.name) {
		tree.selectNode(node, true);
	}
}

$('#btnExpand').click(function() {
	tree.expandAll(true);
});
$('#btnCollapse').click(function() {
	tree.expandAll(false);
});
/**
 * 确定之后执行的函数
 * 
 * @returns
 */
function loadObj() {
	var treeId = $("#treeId").val();
	var treeName = $("#treeName").val();
	// var $doc = layer.getChildFrame("body",
	// layer.getFrameIndex(window.useIframe) );
	// 获取父亲的界面 然后 找到父亲的界面的iframe ，然后操作这个iframe
	// 之前使用的一个的document 元素
	var $beforeDoc = $(parent.$("#" + window.useIframe)[0].contentWindow.document);
	$beforeDoc.find("#" + showIdDom).val(treeId);
	$beforeDoc.find("#" + showNameDom).val(treeName);
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}
// 对搜索框进行操作
var lastValue = "", nodeList = [], key = $("#keyword");
key.bind("focus", focusKey).bind("blur", blurKey).bind("change cut input propertychange", searchNode);
key.bind("keydown", function(e) {
	if (e.which == 13) {
		searchNode();
	}
});

function focusKey(e) {
	if (key.hasClass("empty")) {
		key.removeClass("empty");
	}
}

function blurKey(e) {
	if (key.get(0).value === "") {
		key.addClass("empty");
	}
	searchNode(e);
}

function searchNode() {
	var value = $.trim(key.get(0).value);
	var keyType = treeTitle;
	if (lastValue === value) {
		return;
	}
	lastValue = value;
	var nodes = tree.getNodes();
	if (value == "") {
		showAllNode(nodes);
		return;
	}
	hideAllNode(nodes);
	nodeList = tree.getNodesByParamFuzzy(keyType, value);
	updateNodes(nodeList);
}

function hideAllNode(nodes) {
	var tree = $.fn.zTree.getZTreeObj("tree");
	nodes = tree.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		tree.hideNode(nodes[i]);
	}
}

function showAllNode(nodes) {
	nodes = tree.transformToArray(nodes);
	for (var i = nodes.length - 1; i >= 0; i--) {
		if (nodes[i].getParentNode() != null) {
			tree.expandNode(nodes[i], false, false, false, false);
		} else {
			tree.expandNode(nodes[i], true, true, false, false);
		}
		tree.showNode(nodes[i]);
		showAllNode(nodes[i].children);
	}
}

function updateNodes(nodeList) {
	tree.showNodes(nodeList);
	for (var i = 0, l = nodeList.length; i < l; i++) {
		var treeNode = nodeList[i];
		showChildren(treeNode);
		showParent(treeNode)
	}
}

function showChildren(treeNode) {
	if (treeNode.isParent) {
		for ( var idx in treeNode.children) {
			var node = treeNode.children[idx];
			tree.showNode(node);
			showChildren(node);
		}
	}
}
function showParent(treeNode) {
	var parentNode;
	while ((parentNode = treeNode.getParentNode()) != null) {
		tree.showNode(parentNode);
		tree.expandNode(parentNode, true, false, false);
		treeNode = parentNode;
	}
}

function search($this) {
	$('#search').slideToggle(200);
	$('#btnShow').toggle();
	$('#btnHide').toggle();
	$('#keyword').focus();
}